package controllers

import (
	"WebSvr/modules/mysql"

	"WebSvr/modules/common"
	"WebSvr/modules/memList"
	"encoding/json"
	"fmt"
	"net/http"
	"text/template"
	"time"

	"github.com/astaxie/beego"
)

type ServerListController struct {
	beego.Controller
}

func (this *ServerListController) Prepare() {
	this.EnableXSRF = false
}

func (this *ServerListController) escapeC2SOnlineReport() common.OnlineUserReport {
	var msglogic2Web common.OnlineUserReport
	json.Unmarshal(this.Ctx.Input.RequestBody, &msglogic2Web)

	var msgReport common.OnlineUserReport
	/*转换预防XSS*/
	msgReport.Cycle = uint32(msglogic2Web.Cycle)
	msgReport.LoginNums = uint32(msglogic2Web.LoginNums)
	msgReport.MaxOnLineNums = uint32(msglogic2Web.MaxOnLineNums)
	msgReport.OfflineNums = uint32(msglogic2Web.OfflineNums)
	msgReport.OnlineNums = uint32(msglogic2Web.OnlineNums)
	msgReport.TimeStamp = int64(msglogic2Web.TimeStamp)
	msgReport.ServerName = template.JSEscapeString(msglogic2Web.ServerName)
	return msgReport
}

func (this *ServerListController) escapeC2SSysReport() common.SysReportInfo {
	var msgSysReport common.SysReportInfo
	json.Unmarshal(this.Ctx.Input.RequestBody, &msgSysReport)

	var msgEscapeReport common.SysReportInfo
	/*转换预防XSS*/
	msgEscapeReport.CpuUsage = msgSysReport.CpuUsage
	msgEscapeReport.MemProgUsage = msgSysReport.MemProgUsage
	msgEscapeReport.MemSysUsage = msgSysReport.MemSysUsage
	msgEscapeReport.Samplecycle = uint32(msgSysReport.Samplecycle)
	msgEscapeReport.Sendcycle = uint32(msgSysReport.Sendcycle)
	msgEscapeReport.ServerName = template.JSEscapeString(msgSysReport.ServerName)
	msgEscapeReport.TimeStamp = int64(msgSysReport.TimeStamp)
	msgEscapeReport.TotalMem = uint64(msgSysReport.TotalMem)
	return msgEscapeReport

}
func (this *ServerListController) PostOnlineReport() {
	if !common.GStopFlag {
		var msgReport common.OnlineUserReport
		webAdmin := mysql.GetWebAdmin()
		if common.DebugCtrl == 1 {
			pMapInfo := webAdmin.GetALLInfo()
			if pMapInfo != nil {
				recvStamp := time.Now().Unix()
				for _, serverCfg := range *pMapInfo {
					msgReport.ServerName = serverCfg.Name
					memListMgr := memList.GetMemListMgr()
					memListMgr.Add2serverListMap(serverCfg.Ip, int64(serverCfg.Port), serverCfg.Area, serverCfg.NewServer, &msgReport, recvStamp)
				}
				ip := this.Ctx.Input.IP()
				str := fmt.Sprintf("dev 在线上报成功,ip:%s", ip)
				beego.Info(str)
				this.CustomAbort(http.StatusOK, "onlineUserReport recv ok")
			} else {
				this.CustomAbort(http.StatusNotFound, "webAdmin.GetALLInfo nil")
			}
		} else {
			var authInfo mysql.ServerInfo
			msgReport = this.escapeC2SOnlineReport()
			authInfo.Ip = this.Ctx.Input.IP()
			authInfo.Name = msgReport.ServerName
			authSucc, areaName, bNewServer, port64 := webAdmin.GetAreaName(&authInfo)
			if authSucc {
				recvStamp := time.Now().Unix()
				if msgReport.Cycle > 0 {
					memListMgr := memList.GetMemListMgr()
					memListMgr.Add2serverListMap(authInfo.Ip, port64, areaName, bNewServer, &msgReport, recvStamp)
					this.CustomAbort(http.StatusOK, "onlineUserReport recv ok")
				} else {
					this.CustomAbort(http.StatusNotFound, "onlineUserReport recv err")
				}
			} else {
				this.CustomAbort(http.StatusBadRequest, "onlineUserReport auth err")
			}
		}

	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}

}

func (this *ServerListController) PostSysReport() {
	if !common.GStopFlag {
		msgSysReport := this.escapeC2SSysReport()
		var authInfo mysql.ServerInfo
		authInfo.Ip = this.Ctx.Input.IP()
		authInfo.Name = msgSysReport.ServerName
		webAdmin := mysql.GetWebAdmin()
		authSucc := webAdmin.AuthServer(&authInfo)
		if authSucc {
			//先验证
			if msgSysReport.Sendcycle > 0 {
				pcStatus := mysql.GetPcStatus()
				bsucc := pcStatus.AnalysePcReport(&msgSysReport)
				if bsucc {
					if common.DebugCtrl == 1 {
						str := fmt.Sprintf("系统上报成功,ip:%s,服务器:%s", authInfo.Ip, authInfo.Name)
						beego.Info(str)
					}
					this.CustomAbort(http.StatusOK, "sysReport recv ok")
				} else {
					if common.DebugCtrl == 1 {
						str := fmt.Sprintf("系统上报接收失败,ip:%s,服务器:%s", authInfo.Ip, authInfo.Name)
						beego.Info(str)
					}
					this.CustomAbort(http.StatusNotFound, "PostSysReport recv err")
				}
			} else {
				if common.DebugCtrl == 1 {
					str := fmt.Sprintf("系统上报Sendcycle错误:%d,ip:%s,服务器:%s", msgSysReport.Sendcycle, authInfo.Ip, authInfo.Name)
					beego.Info(str)
				}
				this.CustomAbort(http.StatusNotFound, "sysReport recv err")
			}
		} else {
			str := fmt.Sprintf("系统上报验证失败,ip:%s,服务器:%s", authInfo.Ip, authInfo.Name)
			beego.Info(str)
			this.CustomAbort(http.StatusBadRequest, "sysReport auth err")
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *ServerListController) GetServerList() {
	if !common.GStopFlag {
		memMgr := memList.GetMemListMgr()
		msgGetListStr, err := memMgr.GetServerList()
		if err == nil {
			//改用protobuf推送
			this.CustomAbort(http.StatusOK, msgGetListStr)
		} else {
			this.CustomAbort(http.StatusNotFound, "ServerList err")
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *ServerListController) Finish() {
	requestbody := this.Ctx.Request.Body
	if requestbody != nil {
		defer requestbody.Close()
	}
}
